#include "ResponseAdder.h"
#include "AssetPool.h"
#include "..\core\src\AssetManager.h"

namespace Xps
{
	ResponseAdder::ResponseAdder(int qid, QWidget* parent) : AssetAdder(parent, "Add Response", ":/rs/images/response.png")
	{
		this->qid = qid;
		content = setupContent();
	}

	ResponseAdder::~ResponseAdder(void)
	{
	}


	QWidget* ResponseAdder::setupContent()
	{	
		text = new QLineEdit();

		QGridLayout* content_layout = new QGridLayout();
		content->setLayout(content_layout);
		content_layout->addWidget(text, 1, 0, Qt::AlignTop);
		content_layout->addWidget(new QLabel("Lets create some responses. Yes or no would be the easiest\nexamples, but feel free to be creative."),0, 0, Qt::AlignBottom);
		text->setFocus(Qt::FocusReason::TabFocusReason);
		
		return content;
	}


	void ResponseAdder::accept()
	{
		XpsAssets::AssetManager* am = ((Xps::AssetPool*) parent())->am;
		int rid = am->createResponse(qid);
		
		if (rid == -1) 
		{
			QDialog::accept();
			return;
		}

		XpsAssets::IResponse* r = am->getResponse(rid);

		if (r == NULL)
			return;

		r->setText(text->text());
		QDialog::accept();
	}
}